//
//  RegularUnit.m
//  NetworkTaxiDriver
//
//  Created by thinkjoy on 16/9/21.
//  Copyright © 2016年 Thinkjoy_RS. All rights reserved.
//

#import "RegularUnit.h"

@implementation RegularUnit
/**
 *  是否是手机号
 * 手机号码 13[0-9],14[5|7|9],15[0-3],15[5-9],17[0|1|3|5|6|8],18[0-9]
 * 移动：134[0-8],13[5-9],147,15[0-2],15[7-9],178,18[2-4],18[7-8]
 * 联通：13[0-2],145,15[5-6],17[5-6],18[5-6]
 * 电信：133,1349,149,153,173,177,180,181,189
 * 虚拟运营商: 170[0-2]电信  170[3|5|6]移动 170[4|7|8|9],171 联通
 * 上网卡又称数据卡，14号段为上网卡专属号段，中国联通上网卡号段为145，中国移动上网卡号段为147，中国电信上网卡号段为149
 */
+ (BOOL)isMobilePhone:(NSString *)phoneNum
{
    NSString * MOBIL = @"^1(3[0-9]|4[579]|5[0-35-9]|7[01356]|8[0-9])\\d{8}$";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBIL];
    if ([regextestmobile evaluateWithObject:phoneNum]) {
        return YES;
    }
    return NO;
}

/**
 *  是否是移动号
 * 中国移动：China Mobile
 * 134[0-8],13[5-9],147,15[0-2],15[7-9],170[3|5|6],178,18[2-4],18[7-8]
 */
+ (BOOL)isCMMobilePhone:(NSString *)phoneNum
{
    NSString * CM = @"^1(34[0-8]|70[356]|(3[5-9]|47|5[0-27-9]|78|8[2-47-8])\\d)\\d{7}$";
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    if ([regextestcm evaluateWithObject:phoneNum]) {
        return YES;
    }
    return NO;
}

/**
 *  是否是联通号
 * 中国联通：China Unicom
 * 13[0-2],145,15[5-6],17[5-6],18[5-6],170[4|7|8|9],171
 */
+ (BOOL)isCUMobilePhone:(NSString *)phoneNum
{
    NSString * CU = @"^1(70[07-9]|(3[0-2]|45|5[5-6]|7[15-6]|8[5-6])\\d)\\d{7}$";
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    if ([regextestcu evaluateWithObject:phoneNum]) {
        return YES;
    }
    return NO;
}

/**
 *  是否是电信号
 * 中国电信：China Telecom
 * 133,1349,149,153,173,177,180,181,189,170[0-2]
 */
+ (BOOL)isCTMobilePhone:(NSString *)phoneNum
{
    NSString * CT = @"^1(34[9]|70[0-2]|(33|49|53|7[37]|8[019])\\d)\\d{7}$";
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    if ([regextestct evaluateWithObject:phoneNum]) {
        return YES;
    }
    return NO;
}

#pragma mark    核对密码格式,6~16位数字/字母/下划线
+ (BOOL)checkPsw:(NSString *)pswStr
{
    NSString * pattern  =   @"^[A-Za-z0-9_]{6,16}$";
    NSPredicate * pred  =   [NSPredicate predicateWithFormat:@"SELF MATCHES %@",pattern];
    return [pred evaluateWithObject:pswStr];
}

#pragma mark    18位身份证格式和合法性验证
+ (BOOL)checkUserID:(NSString *)userID
{
    //长度不为18的都排除掉
    if (userID.length!=18) {
        return NO;
    }
    //^(0[13578](0[1-9]|[12]\d|3[01]))|(1[02](0[1-9]|[12]\d|3[01]))|(0[469](0[1-9]|[12]\d|30))|(02(0[1-9]|[1]\d|2[1-9]))|(11(0[1-9]|[12]\d|30))$    月份正则表达式判断，2月没有办法计算晕年，按29天判断
    //校验格式
    NSString *regex2 = @"^(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)|(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[Xx])$)$";
    NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    BOOL flag = [identityCardPredicate evaluateWithObject:userID];
    
    if (!flag) {
        return flag;    //格式错误
    }else {
        //格式正确在判断是否合法
        
        //将前17位加权因子保存在数组里
        NSArray * idCardWiArray = @[@"7", @"9", @"10", @"5", @"8", @"4", @"2", @"1", @"6", @"3", @"7", @"9", @"10", @"5", @"8", @"4", @"2"];
        
        //这是除以11后，可能产生的11位余数、验证码，也保存成数组
        NSArray * idCardYArray = @[@"1", @"0", @"10", @"9", @"8", @"7", @"6", @"5", @"4", @"3", @"2"];
        
        //用来保存前17位各自乖以加权因子后的总和
        NSInteger idCardWiSum = 0;
        for(int i = 0;i < 17;i++)
        {
            NSInteger subStrIndex = [[userID substringWithRange:NSMakeRange(i, 1)] integerValue];
            NSInteger idCardWiIndex = [[idCardWiArray objectAtIndex:i] integerValue];
            
            idCardWiSum+= subStrIndex * idCardWiIndex;
            
        }
        
        //计算出校验码所在数组的位置
        NSInteger idCardMod=idCardWiSum%11;
        
        //得到最后一位身份证号码
        NSString * idCardLast= [userID substringWithRange:NSMakeRange(17, 1)];
        
        //如果等于2，则说明校验码是10，身份证号码最后一位应该是X
        if(idCardMod==2)
        {
            if([idCardLast isEqualToString:@"X"]||[idCardLast isEqualToString:@"x"])
            {
                return YES;
            }else
            {
                return NO;
            }
        }else{
            //用计算出的验证码与最后一位身份证号码匹配，如果一致，说明通过，否则是无效的身份证号码
            if([idCardLast isEqualToString: [idCardYArray objectAtIndex:idCardMod]])
            {
                return YES;
            }
            else
            {
                return NO;
            }
        }
    }
}

#pragma mark    18和15位身份证格式验证
+ (BOOL)checkIDCade:(NSString *)IDCade
{
    {
        NSString *value = [IDCade copy];
        value = [value stringByReplacingOccurrencesOfString:@"X" withString:@"x"];
        value = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        int length = 0;
        if (!value) {
            return NO;
        }else {
            length = (int)value.length;
            if (length != 15 && length !=18) {
                return NO;
            }
        }
        // 省份代码
        NSArray *areasArray =@[@"11", @"12", @"13", @"14", @"15", @"21", @"22", @"23", @"31", @"32", @"33", @"34", @"35", @"36", @"37", @"41", @"42", @"43", @"44", @"45", @"46", @"50", @"51", @"52", @"53", @"54", @"61", @"62", @"63", @"64", @"65", @"71", @"81", @"82", @"91"];
        NSString *valueStart2 = [value substringToIndex:2];
        BOOL areaFlag = NO;
        for (NSString *areaCode in areasArray) {
            if ([areaCode isEqualToString:valueStart2]) {
                areaFlag = YES;
                break;
            }
        }
        if (!areaFlag) {
            return NO;
        }
        NSRegularExpression *regularExpression;
        NSUInteger numberofMatch;
        int year = 0;
        switch (length) {
            case 15:
                year = [value substringWithRange:NSMakeRange(6,2)].intValue +1900;
                if (year % 4 ==0 || (year % 100 ==0 && year % 4 ==0)) {
                    regularExpression = [[NSRegularExpression alloc] initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$"                   options:NSRegularExpressionCaseInsensitive error:nil];// 测试出生日期的合法性
                }else {
                    regularExpression = [[NSRegularExpression alloc] initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$"           options:NSRegularExpressionCaseInsensitive error:nil];// 测试出生日期的合法性
                }
                numberofMatch = [regularExpression numberOfMatchesInString:value options:NSMatchingReportProgress range:NSMakeRange(0, value.length)];
                if(numberofMatch > 0) {
                    return YES;
                }else {
                    return NO;
                }
            case 18:
                year = [value substringWithRange:NSMakeRange(6,4)].intValue;
                if (year % 4 ==0 || (year % 100 ==0 && year % 4 ==0)) {
                    regularExpression = [[NSRegularExpression alloc] initWithPattern:@"^[1-9][0-9]{5}19|20[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$"options:NSRegularExpressionCaseInsensitive error:nil];// 测试出生日期的合法性
                    
                }else {
                    regularExpression = [[NSRegularExpression alloc] initWithPattern:@"^[1-9][0-9]{5}19|20[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$"
                                                                             options:NSRegularExpressionCaseInsensitive error:nil];// 测试出生日期的合法性
                }
                numberofMatch = [regularExpression numberOfMatchesInString:value options:NSMatchingReportProgress range:NSMakeRange(0, value.length)];
                if(numberofMatch > 0) {
                    int S = ([value substringWithRange:NSMakeRange(0,1)].intValue + [value substringWithRange:NSMakeRange(10,1)].intValue) *7 + ([value substringWithRange:NSMakeRange(1,1)].intValue + [value substringWithRange:NSMakeRange(11,1)].intValue) *9 + ([value substringWithRange:NSMakeRange(2,1)].intValue + [value substringWithRange:NSMakeRange(12,1)].intValue) *10 + ([value substringWithRange:NSMakeRange(3,1)].intValue + [value substringWithRange:NSMakeRange(13,1)].intValue) *5 + ([value substringWithRange:NSMakeRange(4,1)].intValue + [value substringWithRange:NSMakeRange(14,1)].intValue) *8 + ([value substringWithRange:NSMakeRange(5,1)].intValue + [value substringWithRange:NSMakeRange(15,1)].intValue) *4 + ([value substringWithRange:NSMakeRange(6,1)].intValue + [value substringWithRange:NSMakeRange(16,1)].intValue) *2 + [value substringWithRange:NSMakeRange(7,1)].intValue *1 + [value substringWithRange:NSMakeRange(8,1)].intValue *6 + [value substringWithRange:NSMakeRange(9,1)].intValue *3;
                    int Y = S % 11;
                    NSString *M = @"F";
                    NSString *JYM = @"10X98765432";
                    M = [JYM substringWithRange:NSMakeRange(Y,1)]; // 判断校验位
                    if ([M isEqualToString:[[value substringWithRange:NSMakeRange(17,1)] uppercaseString]]) {
                        return YES;// 检测ID的校验位
                    }else {
                        return NO;
                    }
                }else {
                    return NO;
                }
                
            default:
                return NO;
        }
        return NO;
    }
}


#pragma mark    车牌号格式校验
+ (BOOL)checkCarID:(NSString *)carID;
{
    if (carID.length!=7) {
        return NO;
    }
    NSString *carRegex = @"^[\u4e00-\u9fa5][A-Ha-hJ-Zj-z][A-Ha-hJ-Zj-z\d]{4}[\u4e00-\u9fa5A-Ha-hJ-Zj-z\d]$";
    NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",carRegex];
    return [carTest evaluateWithObject:carID];
    
    return YES;
}

#pragma mark    邮箱格式校验
+ (BOOL)checkEmail:(NSString *)email
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}

#pragma mark   /** 邮政编码 */
+ (BOOL)checkUrl:(NSString *)url
{
    NSString *regex = @"^((http)|(https))+:[^\\s]+\\.[^\\s]*$";
    NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [pre evaluateWithObject:url];
}

#pragma mark   /** 纯汉字 */
+ (BOOL)checkChinese:(NSString *)chinese
{
    NSString *phoneRegex = @"^[\u4e00-\u9fa5]+$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:chinese];
}

#pragma mark    是否为IP
+ (BOOL)checkIP:(NSString *)ip;
{
    NSString *regex = [NSString stringWithFormat:@"^((2([0-4]\\d|5[0-5])|[0-1]\\d{2})\\.){3}(2([0-4]\\d|5[0-5])|[0-1]?\\d{1,2})$"];
    //@"^((2[0-4]\\d|25[0-5]|[0-1]?\\d{1,2})\\.){3}(2[0-4]\\d|25[0-5]|[0-1]?\\d{1,2})$"
    NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    BOOL rc = [pre evaluateWithObject:self];
    
    if (rc) {
        NSArray *componds = [ip componentsSeparatedByString:@","];
        
        BOOL v = YES;
        for (NSString *s in componds) {
            if (s.integerValue > 255) {
                v = NO;
                break;
            }
        }
        
        return v;
    }
    
    return NO;
}


#pragma mark    工商税号
+ (BOOL)checkTaxNo:(NSString *)taxNo
{
    NSString *emailRegex = @"[0-9]\\d{13}([0-9]|X)$";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:taxNo];
}

#pragma mark    去掉两端空格和换行符
/** 去掉两端空格和换行符 */
+ (NSString *)stringByTrimmingBlank:(NSString *)str
{
    return [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

#pragma mark    去掉html格式
/** 去掉html格式 */
+ (NSString *)removeHtmlFormat:(NSString *)str
{
    NSError *error;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<[^>]*>" options:NSRegularExpressionCaseInsensitive error:&error];
    if (!error) {
        str = [regex stringByReplacingMatchesInString:str options:0 range:NSMakeRange(0, str.length) withTemplate:@"$2$1"];
    } else {
        NSLog(@"%@", error);
    }
    
    NSArray *html_code = @[
                           @"\"", @"'", @"&", @"<", @">",
                           @"", @"¡", @"¢", @"£", @"¤",
                           @"¥", @"¦", @"§", @"¨", @"©",
                           @"ª", @"«", @"¬", @"", @"®",
                           @"¯", @"°", @"±", @"²", @"³",
                           
                           @"´", @"µ", @"¶", @"·", @"¸",
                           @"¹", @"º", @"»", @"¼", @"½",
                           @"¾", @"¿", @"×", @"÷", @"À",
                           @"Á", @"Â", @"Ã", @"Ä", @"Å",
                           @"Æ", @"Ç", @"È", @"É", @"Ê",
                           
                           @"Ë", @"Ì", @"Í", @"Î", @"Ï",
                           @"Ð", @"Ñ", @"Ò", @"Ó", @"Ô",
                           @"Õ", @"Ö", @"Ø", @"Ù", @"Ú",
                           @"Û", @"Ü", @"Ý", @"Þ", @"ß",
                           @"à", @"á", @"â", @"ã", @"ä",
                           
                           @"å", @"æ", @"ç", @"è", @"é",
                           @"ê", @"ë", @"ì", @"í", @"î",
                           @"ï", @"ð", @"ñ", @"ò", @"ó",
                           @"ô", @"õ", @"ö", @"ø", @"ù",
                           @"ú", @"û", @"ü", @"ý", @"þ",
                           
                           @"ÿ", @"∀", @"∂", @"∃", @"∅",
                           @"∇", @"∈", @"∉", @"∋", @"∏",
                           @"∑", @"−", @"∗", @"√", @"∝",
                           @"∞", @"∠", @"∧", @"∨", @"∩",
                           @"∪", @"∫", @"∴", @"∼", @"≅",
                           
                           @"≈", @"≠", @"≡", @"≤", @"≥",
                           @"⊂", @"⊃", @"⊄", @"⊆", @"⊇",
                           @"⊕", @"⊗", @"⊥", @"⋅", @"Α",
                           @"Β", @"Γ", @"Δ", @"Ε", @"Ζ",
                           @"Η", @"Θ", @"Ι", @"Κ", @"Λ",
                           
                           @"Μ", @"Ν", @"Ξ", @"Ο", @"Π",
                           @"Ρ", @"Σ", @"Τ", @"Υ", @"Φ",
                           @"Χ", @"Ψ", @"Ω", @"α", @"β",
                           @"γ", @"δ", @"ε", @"ζ", @"η",
                           @"θ", @"ι", @"κ", @"λ", @"μ",
                           
                           @"ν", @"ξ", @"ο", @"π", @"ρ",
                           @"ς", @"σ", @"τ", @"υ", @"φ",
                           @"χ", @"ψ", @"ω", @"ϑ", @"ϒ",
                           @"ϖ", @"Œ", @"œ", @"Š", @"š",
                           @"Ÿ", @"ƒ", @"ˆ", @"˜", @"",
                           
                           @"", @"", @"", @"", @"",
                           @"", @"–", @"—", @"‘", @"’",
                           @"‚", @"“", @"”", @"„", @"†",
                           @"‡", @"•", @"…", @"‰", @"′",
                           @"″", @"‹", @"›", @"‾", @"€",
                           
                           @"™", @"←", @"↑", @"→", @"↓",
                           @"↔", @"↵", @"⌈", @"⌉", @"⌊",
                           @"⌋", @"◊", @"♠", @"♣", @"♥",
                           @"♦",
                           ];
    NSArray *code = @[
                      @"&quot;", @"&apos;", @"&amp;", @"&lt;", @"&gt;",
                      @"&nbsp;", @"&iexcl;", @"&cent;", @"&pound;", @"&curren;",
                      @"&yen;", @"&brvbar;", @"&sect;", @"&uml;", @"&copy;",
                      @"&ordf;", @"&laquo;", @"&not;", @"&shy;", @"&reg;",
                      @"&macr;", @"&deg;", @"&plusmn;", @"&sup2;", @"&sup3;",
                      
                      @"&acute;", @"&micro;", @"&para;", @"&middot;", @"&cedil;",
                      @"&sup1;", @"&ordm;", @"&raquo;", @"&frac14;", @"&frac12;",
                      @"&frac34;", @"&iquest;", @"&times;", @"&divide;", @"&Agrave;",
                      @"&Aacute;", @"&Acirc;", @"&Atilde;", @"&Auml;", @"&Aring;",
                      @"&AElig;", @"&Ccedil;", @"&Egrave;", @"&Eacute;", @"&Ecirc;",
                      
                      @"&Euml;", @"&Igrave;", @"&Iacute;", @"&Icirc;", @"&Iuml;",
                      @"&ETH;", @"&Ntilde;", @"&Ograve;", @"&Oacute;", @"&Ocirc;",
                      @"&Otilde;", @"&Ouml;", @"&Oslash;", @"&Ugrave;", @"&Uacute;",
                      @"&Ucirc;", @"&Uuml;", @"&Yacute;", @"&THORN;", @"&szlig;",
                      @"&agrave;", @"&aacute;", @"&acirc;", @"&atilde;", @"&auml;",
                      
                      @"&aring;", @"&aelig;", @"&ccedil;", @"&egrave;", @"&eacute;",
                      @"&ecirc;", @"&euml;", @"&igrave;", @"&iacute;", @"&icirc;",
                      @"&iuml;", @"&eth;", @"&ntilde;", @"&ograve;", @"&oacute;",
                      @"&ocirc;", @"&otilde;", @"&ouml;", @"&oslash;", @"&ugrave;",
                      @"&uacute;", @"&ucirc;", @"&uuml;", @"&yacute;", @"&thorn;",
                      
                      @"&yuml;", @"&forall;", @"&part;", @"&exists;", @"&empty;",
                      @"&nabla;", @"&isin;", @"&notin;", @"&ni;", @"&prod;",
                      @"&sum;", @"&minus;", @"&lowast;", @"&radic;", @"&prop;",
                      @"&infin;", @"&ang;", @"&and;", @"&or;", @"&cap;",
                      @"&cup;", @"&int;", @"&there4;", @"&sim;", @"&cong;",
                      
                      @"&asymp;", @"&ne;", @"&equiv;", @"&le;", @"&ge;",
                      @"&sub;", @"&sup;", @"&nsub;", @"&sube;", @"&supe;",
                      @"&oplus;", @"&otimes;", @"&perp;", @"&sdot;", @"&Alpha;",
                      @"&Beta;", @"&Gamma;", @"&Delta;", @"&Epsilon;", @"&Zeta;",
                      @"&Eta;", @"&Theta;", @"&Iota;", @"&Kappa;", @"&Lambda;",
                      
                      @"&Mu;", @"&Nu;", @"&Xi;", @"&Omicron;", @"&Pi;",
                      @"&Rho;", @"&Sigma;", @"&Tau;", @"&Upsilon;", @"&Phi;",
                      @"&Chi;", @"&Psi;", @"&Omega;", @"&alpha;", @"&beta;",
                      @"&gamma;", @"&delta;", @"&epsilon;", @"&zeta;", @"&eta;",
                      @"&theta;", @"&iota;", @"&kappa;", @"&lambda;", @"&mu;",
                      
                      @"&nu;", @"&xi;", @"&omicron;", @"&pi;", @"&rho;",
                      @"&sigmaf;", @"&sigma;", @"&tau;", @"&upsilon;", @"&phi;",
                      @"&chi;", @"&psi;", @"&omega;", @"&thetasym;", @"&upsih;",
                      @"&piv;", @"&OElig;", @"&oelig;", @"&Scaron;", @"&scaron;",
                      @"&Yuml;", @"&fnof;", @"&circ;", @"&tilde;", @"&ensp;",
                      
                      @"&emsp;", @"&thinsp;", @"&zwnj;", @"&zwj;", @"&lrm;",
                      @"&rlm;", @"&ndash;", @"&mdash;", @"&lsquo;", @"&rsquo;",
                      @"&sbquo;", @"&ldquo;", @"&rdquo;", @"&bdquo;", @"&dagger;",
                      @"&Dagger;", @"&bull;", @"&hellip;", @"&permil;", @"&prime;",
                      @"&Prime;", @"&lsaquo;", @"&rsaquo;", @"&oline;", @"&euro;",
                      
                      @"&trade;", @"&larr;", @"&uarr;", @"&rarr;", @"&darr;",
                      @"&harr;", @"&crarr;", @"&lceil;", @"&rceil;", @"&lfloor;",
                      @"&rfloor;", @"&loz;", @"&spades;", @"&clubs;", @"&hearts;",
                      @"&diams;",
                      ];
    NSArray *code_hex = @[
                          @"&#34;", @"&#39;", @"&#38;", @"&#60;", @"&#62;",
                          @"&#160;", @"&#161;", @"&#162;", @"&#163;", @"&#164;",
                          @"&#165;", @"&#166;", @"&#167;", @"&#168;", @"&#169;",
                          @"&#170;", @"&#171;", @"&#172;", @"&#173;", @"&#174;",
                          @"&#175;", @"&#176;", @"&#177;", @"&#178;", @"&#179;",
                          
                          @"&#180;", @"&#181;", @"&#182;", @"&#183;", @"&#184;",
                          @"&#185;", @"&#186;", @"&#187;", @"&#188;", @"&#189;",
                          @"&#190;", @"&#191;", @"&#215;", @"&#247;", @"&#192;",
                          @"&#193;", @"&#194;", @"&#195;", @"&#196;", @"&#197;",
                          @"&#198;", @"&#199;", @"&#200;", @"&#201;", @"&#202;",
                          
                          @"&#203;", @"&#204;", @"&#205;", @"&#206;", @"&#207;",
                          @"&#208;", @"&#209;", @"&#210;", @"&#211;", @"&#212;",
                          @"&#213;", @"&#214;", @"&#216;", @"&#217;", @"&#218;",
                          @"&#219;", @"&#220;", @"&#221;", @"&#222;", @"&#223;",
                          @"&#224;", @"&#225;", @"&#226;", @"&#227;", @"&#228;",
                          
                          @"&#229;", @"&#230;", @"&#231;", @"&#232;", @"&#233;",
                          @"&#234;", @"&#235;", @"&#236;", @"&#237;", @"&#238;",
                          @"&#239;", @"&#240;", @"&#241;", @"&#242;", @"&#243;",
                          @"&#244;", @"&#245;", @"&#246;", @"&#248;", @"&#249;",
                          @"&#250;", @"&#251;", @"&#252;", @"&#253;", @"&#254;",
                          
                          @"&#255;", @"&#8704;", @"&#8706;", @"&#8707;", @"&#8709;",
                          @"&#8711;", @"&#8712;", @"&#8713;", @"&#8715;", @"&#8719;",
                          @"&#8721;", @"&#8722;", @"&#8727;", @"&#8730;", @"&#8733;",
                          @"&#8734;", @"&#8736;", @"&#8743;", @"&#8744;", @"&#8745;",
                          @"&#8746;", @"&#8747;", @"&#8756;", @"&#8764;", @"&#8773;",
                          
                          @"&#8776;", @"&#8800;", @"&#8801;", @"&#8804;", @"&#8805;",
                          @"&#8834;", @"&#8835;", @"&#8836;", @"&#8838;", @"&#8839;",
                          @"&#8853;", @"&#8855;", @"&#8869;", @"&#8901;", @"&#913;",
                          @"&#914;", @"&#915;", @"&#916;", @"&#917;", @"&#918;",
                          @"&#919;", @"&#920;", @"&#921;", @"&#922;", @"&#923;",
                          
                          @"&#924;", @"&#925;", @"&#926;", @"&#927;", @"&#928;",
                          @"&#929;", @"&#931;", @"&#932;", @"&#933;", @"&#934;",
                          @"&#935;", @"&#936;", @"&#937;", @"&#945;", @"&#946;",
                          @"&#947;", @"&#948;", @"&#949;", @"&#950;", @"&#951;",
                          @"&#952;", @"&#953;", @"&#954;", @"&#923;", @"&#956;",
                          
                          @"&#925;", @"&#958;", @"&#959;", @"&#960;", @"&#961;",
                          @"&#962;", @"&#963;", @"&#964;", @"&#965;", @"&#966;",
                          @"&#967;", @"&#968;", @"&#969;", @"&#977;", @"&#978;",
                          @"&#982;", @"&#338;", @"&#339;", @"&#352;", @"&#353;",
                          @"&#376;", @"&#402;", @"&#710;", @"&#732;", @"&#8194;",
                          
                          @"&#8195;", @"&#8201;", @"&#8204;", @"&#8205;", @"&#8206;",
                          @"&#8207;", @"&#8211;", @"&#8212;", @"&#8216;", @"&#8217;",
                          @"&#8218;", @"&#8220;", @"&#8221;", @"&#8222;", @"&#8224;",
                          @"&#8225;", @"&#8226;", @"&#8230;", @"&#8240;", @"&#8242;",
                          @"&#8243;", @"&#8249;", @"&#8250;", @"&#8254;", @"&#8364;",
                          
                          @"&#8482;", @"&#8592;", @"&#8593;", @"&#8594;", @"&#8595;",
                          @"&#8596;", @"&#8629;", @"&#8968;", @"&#8969;", @"&#8970;",
                          @"&#8971;", @"&#9674;", @"&#9824;", @"&#9827;", @"&#9829;",
                          @"&#9830;",
                          ];
    
    NSInteger idx = 0;
    for (NSString *obj in code) {
        str = [str stringByReplacingOccurrencesOfString:(NSString *)obj withString:html_code[idx]];
        idx++;
    }
    return str;
}


#pragma mark     是否符合最小长度、最长长度，是否包含中文,首字母是否可以为数字
+ (BOOL)checkStr:(NSString *)str withMinLenth:(NSInteger)minLenth
        maxLenth:(NSInteger)maxLenth
  containChinese:(BOOL)containChinese
firstCannotBeDigtal:(BOOL)firstCannotBeDigtal
{
    NSString *hanzi = containChinese ? @"\u4e00-\u9fa5" : @"";
    NSString *first = firstCannotBeDigtal ? @"^[a-zA-Z_]" : @"";
    
    NSString *regex = [NSString stringWithFormat:@"%@[%@A-Za-z0-9_]{%d,%d}", first, hanzi, (int)(minLenth-1), (int)(maxLenth-1)];
    NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [pre evaluateWithObject:str];
}


#pragma mark    是否符合最小长度、最长长度，是否包含中文,数字，字母，其他字符，首字母是否可以为数字
+ (BOOL)checkStr:(NSString *)str withMinLenth:(NSInteger)minLenth maxLenth:(NSInteger)maxLenth containChinese:(BOOL)containChinese containDigtal:(BOOL)containDigtal containLetter:(BOOL)containLetter containOtherCharacter:(NSString *)containOtherCharacter firstCannotBeDigtal:(BOOL)firstCannotBeDigtal
{
    NSString *hanzi = containChinese ? @"\u4e00-\u9fa5" : @"";
    NSString *first = firstCannotBeDigtal ? @"^[a-zA-Z_]" : @"";
    NSString *lengthRegex = [NSString stringWithFormat:@"(?=^.{%@,%@}$)", @(minLenth), @(maxLenth)];
    NSString *digtalRegex = containDigtal ? @"(?=(.*\\d.*){1})" : @"";
    NSString *letterRegex = containLetter ? @"(?=(.*[a-zA-Z].*){1})" : @"";
    NSString *characterRegex = [NSString stringWithFormat:@"(?:%@[%@A-Za-z0-9%@]+)", first, hanzi, containOtherCharacter ? containOtherCharacter : @""];
    NSString *regex = [NSString stringWithFormat:@"%@%@%@%@", lengthRegex, digtalRegex, letterRegex, characterRegex];
    NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [pre evaluateWithObject:str];
}
























@end
